package pl.agh.convexhulltutor.gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JPanel;

import pl.agh.convexhulltutor.structures.Line;
import pl.agh.convexhulltutor.structures.Point;

@SuppressWarnings("serial")
public class DrawPanel extends JPanel {
	public static final int DRAW_PANEL_MARGIN = 30;

	private List<Point> points = new ArrayList<Point>();
	private List<Line> lines = new ArrayList<Line>();

	public DrawPanel() {
		addMouseListener(new MouseListener() {
			@Override
			public void mouseReleased(MouseEvent e) {
				if (!ConvexHullFrame.isAlgoritmRunning()) {
					if (e.getX() >= DRAW_PANEL_MARGIN
							&& e.getX() <= getWidth() - DRAW_PANEL_MARGIN
							&& e.getY() >= DRAW_PANEL_MARGIN
							&& e.getY() <= getHeight() - DRAW_PANEL_MARGIN) {
						if (e.getButton() == MouseEvent.BUTTON1) {
							points.add(new Point(e.getX(), e.getY()));
						} else {
							Iterator<Point> pointIterator = points.iterator();
							Point currentPoint;

							while (pointIterator.hasNext()) {
								currentPoint = pointIterator.next();
								if (currentPoint.isOnPoint(e.getX(), e.getY())) {
									pointIterator.remove();
								}
							}
						}
						repaint();
					} else {
						MessageBox
								.warn("You can't put point so close to the edge");
					}
				} else {
					MessageBox
							.warn("You can't modify points - algorithm is running");
				}
			}

			@Override
			public void mousePressed(MouseEvent e) {
			}

			@Override
			public void mouseExited(MouseEvent e) {
			}

			@Override
			public void mouseEntered(MouseEvent e) {
			}

			@Override
			public void mouseClicked(MouseEvent e) {
			}
		});
	}

	@Override
	public void paintComponent(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;

		g2.setColor(Color.GRAY);
		g2.fillRect(0, 0, getWidth(), getHeight());

		for (Point point : points) {
			g2.setColor(point.getColor());
			g2.fill(point.getEllipse());
		}
		for (Line line : lines) {
			g2.setColor(line.getColor());
			g2.drawLine((int) line.getX1(), (int) line.getY1(),
					(int) line.getX2(), (int) line.getY2());
		}
	}

	public void addPoint(Point point) {
		boolean duplicate = false;
		for (Point oldPoint : points) {
			if (point.isIdentical(oldPoint)) {
				duplicate = true;
			}
		}
		if (!duplicate) {
			points.add(point);
		}
	}

	public List<Point> getPoints() {
		return points;
	}

	public void setPoints(List<Point> points) {
		this.points = points;
	}

	public List<Line> getLines() {
		return lines;
	}

	public void setLines(List<Line> lines) {
		this.lines = lines;
	}

	public void restart() {
		for (Point point : points) {
			point.setColor(Point.DEFAULT_COLOR);
		}
		lines = new ArrayList<Line>();
		repaint();
	}
}
